<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web Tester</title>

    <script src='assets/beta3/eosjs-api.js'></script>
    <script src='assets/beta3/eosjs-jsonrpc.js'></script>
    <script src="assets/beta2-/eosjs-debug.js"></script>
    <script src="assets/beta2-/eosjs-jsonrpc-debug.js"></script>
    <script src="../../bundles/scatterjs-core.min.js"></script>

    <!--<script>-->
	    <!--window.lynxMobile = {-->
            <!--requestSetAccountName:x => {-->
	            <!--const accounts = [{-->
		            <!--name:'testaccount1',-->
		            <!--authority:'active',-->
		            <!--publicKey:'EOS5Jzkpde9pDNTPjyWqcHwvNkrXQma8VPV9qZ3fNu5dvZkiLEKxG',-->
		            <!--blockchain:'eos',-->
		            <!--isHardware:false,-->
		            <!--chainId:'EOS5Jzkpde9pDNTPjyWqcHwvNkrXQma8VPV9qZ3fNu5dvZkiLEKxG'-->
	            <!--}];-->

	            <!--const identity = {-->
		            <!--name:accounts[0].name,-->
		            <!--accounts,-->
		            <!--publicKey:'EOS8RFgis6KAbChv89L3ibPmSH9raqN3iaFWbyLrDgmAuV3rsZ9SM'-->
	            <!--};-->

	            <!--ScatterJS.scatter.identity = identity;-->
	            <!--console.log(ScatterJS.scatter.identity);-->
	            <!--return identity;-->
            <!--},-->
		    <!--requestSetAccount:name => {},-->
            <!--requestArbitrarySignature:x => { console.log(x); },-->
		    <!--requestSignature:x => { console.log(x); }-->
	    <!--};-->
    <!--</script>-->

    <script src="../../bundles/scatterjs-plugin-eosjs2.min.js"></script>
    <script src="../../bundles/scatterjs-plugin-lynx.min.js"></script>
</head>
<body>
<div id="status"></div>
<br>
<br>

<button onclick="login()">Get Identity</button>
<button onclick="logout()">Forget Identity</button>
<button onclick="transfer()">Transfer</button>
<button onclick="hookTransfer()">Hook Transfer</button>
<button onclick="authenticate()">Authenticate</button>

<script>



	ScatterJS.plugins( new ScatterEOS(), new ScatterLynx({Api:eosjs_api.default, JsonRpc:eosjs_jsonrpc2.default}) );

	const network = ScatterJS.Network.fromJson({
		blockchain:'eos',
		chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
		host:'nodes.get-scatter.com',
		port:443,
		protocol:'https'
	});

    // const network = ScatterJS.Network.fromJson({
    //     blockchain:'eos',
    //     chainId:'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
    //     host:'192.168.1.6',
    //     port:8888,
    //     protocol:'http'
    // });

	// const network = ScatterJS.Network.fromJson({
    //     blockchain:'eos',
    //     chainId:'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473',
    //     host:'dev.cryptolions.io',
    //     port:18888,
    //     protocol:'http'
	// });

	let scatter = null;

	const beta3 = false;

	const rpc = beta3
		? new eosjs_jsonrpc2.default(network.fullhost())
		: new eosjs_jsonrpc.JsonRpc(network.fullhost());

	const getApi = (signatureProvider) => beta3
		? new eosjs_api.default({ rpc, signatureProvider })
		: new eosjs.Api({ rpc, signatureProvider });

	const getScatterEos = () => beta3
		? scatter.eos(network, eosjs_api.default, {rpc, beta3:true})
		: scatter.eos(network, eosjs.Api, {rpc});




	const setStatus = () => {
		const status = document.getElementById('status');
		if(!scatter) return status.innerText = 'No Scatter';
		if(!scatter.identity) return status.innerText = 'No Identity';
		status.innerText = scatter.identity.name;
	};

	setStatus();
	setInterval(() => {
		setStatus();
	}, 50);

	ScatterJS.scatter.connect('eosjs2-test', {network}).then(connected => {
		if(!connected) return false;
		scatter = ScatterJS.scatter;
	});

	window.login = async () => await scatter.login();
	window.logout = () => scatter.logout();

	window.transfer = async () => {
		const api = getScatterEos();
		tryTransfer(api);
	};

	window.hookTransfer = async () => {
		const api = getApi(scatter.eosHook(network));
		const from = scatter.identity.accounts[0];
		tryTransfer(api);
	};

	window.authenticate = async () => {
		const getRandom = () => Math.round(Math.random() * 8 + 1).toString();
		let randomString = '';
		for(let i = 0; i < 12; i++) randomString += getRandom();
		console.log('randomString', randomString);
		await scatter.authenticate(randomString).then(res => console.log(res));
	};

	const tryTransfer = async api => {
		try {
			const from = scatter.account('eos');
			const result = await api.transact({
				actions: [{
					account: 'eosio.token',
					name: 'transfer',
					authorization: [{
						actor: from.name,
						permission: from.authority,
					}],
					data: {
						from: from.name,
						to: 'safetransfer',
						quantity: '0.0001 EOS',
						memo: from.name,
					},
				}]
			}, {
				blocksBehind: 3,
				expireSeconds: 30,
			});
			console.log('result', result);
		} catch (e) {
			console.error(e);
		}
	}

</script>
</body>
</html>